package com.jiangyg.mall.ware.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.support.validation.Business1Group;
import com.jiangyg.mall.core.support.validation.SaveOrUpdateGroup;
import com.jiangyg.mall.ware.dto.PurchaseDTO;
import com.jiangyg.mall.ware.service.PurchaseService;
import com.jiangyg.mall.ware.vo.PurchaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：采购信息控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:41:17
 */
@RestController
@RequestMapping("/ware/purchase")
public class PurchaseController {

    /**
     * 采购信息接口
     */
    private final PurchaseService purchaseService;

    @Autowired
    public PurchaseController(PurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }

    /**
     * 功能描述：采购信息分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页采购信息
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<PurchaseVO>> select(HttpServletRequest request, @RequestBody Pagination<PurchaseVO> pagination) throws Exception {
        return Result.ok(purchaseService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改采购信息
     *
     * @param request 请求
     * @param dto     采购信息
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated(SaveOrUpdateGroup.class) PurchaseDTO dto) throws Exception {
        return Result.saveOrUpdate(purchaseService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取采购信息
     *
     * @param request    请求
     * @param purchaseId 采购信息ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<PurchaseVO> info(HttpServletRequest request, @RequestParam(name = "purchaseId") Long purchaseId) throws Exception {
        return Result.ok(purchaseService.selectById(purchaseId));
    }

    /**
     * 功能描述：删除采购信息
     *
     * @param request     请求
     * @param purchaseIds 批量删除的采购信息ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> purchaseIds) {
        return Result.delete(purchaseService.delete(purchaseIds));
    }

    /**
     * 功能描述：采购单人员分配
     *
     * @param request 请求
     * @param dto     采购信息
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/assignee")
    public Result<Object> assignee(HttpServletRequest request, @RequestBody @Validated(Business1Group.class) PurchaseDTO dto) {
        if (purchaseService.assignee(dto)) {
            return Result.ok("采购单分配成功！");
        }
        return Result.error("采购单分配失败！");
    }

    /**
     * 功能描述：查询未分配采购单列表
     *
     * @param wareId  仓库ID
     * @param request 请求
     * @return 返回分页采购信息
     */
    @ResponseBody
    @PostMapping("/unassigned-list")
    public Result<List<PurchaseVO>> unassignedList(HttpServletRequest request, @RequestParam("wareId") Long wareId) throws Exception {
        return Result.ok(purchaseService.unassignedList(wareId));
    }

    /**
     * 功能描述：采购单人员领取
     *
     * @param request     请求
     * @param purchaseIds 采购信息ID集合
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/received")
    public Result<Object> received(HttpServletRequest request, @RequestBody List<Long> purchaseIds) {
        if (purchaseService.received(purchaseIds)) {
            return Result.ok("采购单领取成功！");
        }
        return Result.error("采购单领取失败！");
    }

    /**
     * 功能描述：采购任务完成
     *
     * @param request     请求
     * @param purchaseIds 采购信息ID集合
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/finished")
    public Result<Object> finished(HttpServletRequest request, @RequestBody List<Long> purchaseIds) {
        if (purchaseService.finished(purchaseIds)) {
            return Result.ok("采购任务成功！");
        }
        return Result.error("采购任务成功！");
    }

}
